package com.nbst.controller;

import com.nbst.model.People;
import com.nbst.model.SendMail;
import com.nbst.service.PeopleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;


@Controller
public class BaseController {

    @Autowired
    PeopleService peopleService;

    @RequestMapping("/login")
    public String index(){
        return "login";
    }

    @RequestMapping("/hello")
    public String hello(){
        return "hello";
    }

    @RequestMapping("/email")
    public String activation(@RequestParam(value="name") String name,@RequestParam(value="new1_psw") String new1_psw,
                             @RequestParam(value="new2_psw") String new2_psw){
        People people=peopleService.selectByPrimaryKey(name);
        if(people!=null){
            return "re-enroll";
        }
        if(new1_psw.equals(new2_psw)) {
            People user=new People();
            user.setName(name);
            user.setPassword(new1_psw);
            peopleService.insertUser(user);
            String url = "http://localhost:8080/activation?id=" + name;
            SendMail.send(name, url);

            return "enroll";
        }else{
            return "login";
        }
    }
    @RequestMapping("/activation")
    public String activation(@RequestParam(value="id") String name){
        People people=peopleService.selectByPrimaryKey(name);
        int change=((int)(people.getTag()));
        System.out.print(people);
        if(people!=null&&change==0){
            people.setTag(1);
            peopleService.updateByPrimaryKey(people);
        }
        return "activation";
    }
    @RequestMapping("/people")
    public String people(People record){
        System.out.print(record.getName());
        People people=peopleService.selectByPrimaryKey(record.getName());
        System.out.print(people);
        if(people.getPassword().equals(record.getPassword())){
            return "hello";
        }else{
            return "login";
        }
    }
    @RequestMapping("/login1")
    public String login(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {
        String str=req.getParameter("name");
        String pwd=req.getParameter("password");
        People people=peopleService.selectByPrimaryKey(str);
        System.out.print(people);
        if(people!=null&&pwd.equals(people.getPassword())&&people.getTag()==1) {
            req.getSession().setAttribute("user", people);
            People p = (People) req.getSession().getAttribute("user");
            PrintWriter pw = resp.getWriter();
            pw.write(people.getName());
        }else{
            return "login_failed";
        }
        return "hello";
    }
}
